//
//  LVCategoryContent.m
//  TestAnimation
//
//  Created by guantianpeng on 13-5-11.
//  Copyright (c) 2013年 baidu.com. All rights reserved.
//

#import "LVCategoryContent.h"
#import "LVCategoryCell.h"
#import "TravelException.h"
#import "ImageDefs.h"
#import <QuartzCore/QuartzCore.h>
#import "BaseUIExt.h"

static const CGFloat kLVCategoryCellMargin = 10;

@interface  LVCategoryContent()

@property(nonatomic, retain) LVCategoryCell *previousCellSelected;
@property(nonatomic, retain) LVCategoryCell *currentCellSelected;
@property(nonatomic, retain) NSMutableArray *categoryCells;

- (void)onClick:(id)sender;

@end

@implementation LVCategoryContent

@synthesize previousCellSelected = _previousCellSelected;
@synthesize currentCellSelected = _currentCellSelected;
@synthesize categoryCellDelegate = _categoryCellDelegate;
@synthesize contentHeight = _contentHeight;
@synthesize categoryList = _categoryList;
@synthesize categoryCells = _categoryCells;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColorWithImage:kImgCategoryConentBg];
        _categoryCells = [[NSMutableArray alloc] initWithCapacity:1];
        self.indicatorStyle  = UIScrollViewIndicatorStyleWhite;
        self.xMargin = kLVCategoryCellMargin;
        self.contentInset = UIEdgeInsetsMake(0, kLVCategoryCellMargin, 0, kLVCategoryCellMargin);
    }
    return self;
}

- (void)setCategoryList:(NSArray *)categoryList
{
    CGFloat startX = self.contentInset.left;
    CGFloat totalW = 0.0;
    [_categoryCells removeAllObjects];
    for (CategoryCellInfo *info in categoryList) {
        LVCategoryCell *cell = [[LVCategoryCell alloc]initWithFrame:CGRectZero];
        [cell setCateCellInfo:info];
        
        CGPoint origin = CGPointMake(startX, (_contentHeight - cell.bounds.size.height) / 2);
        cell.frame = CGRectMake(origin.x, origin.y, cell.bounds.size.width, cell.bounds.size.height);
        startX += cell.bounds.size.width + self.xMargin;
        
        [cell addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        if ( [info.cId isEqualToString:@"全部"] || [info.cId isEqualToString:@"rating"] || [info.cId isEqualToString:@"default"] ) {
            self.previousCellSelected = nil;
            self.currentCellSelected = cell;
            cell.selected = YES;
        }
        [self addSubview:cell];
        [_categoryCells addObject:cell];
        [cell release];
    }
    totalW = startX;
    self.contentSize = CGSizeMake(totalW, _contentHeight);
    [_categoryList release];
    _categoryList = [categoryList retain];
}

-(void) selectAtIndex:(NSUInteger) index {
    LVCategoryCell *cell = [_categoryCells objectAtIndex:index];
    [self onClick:cell];
}
- (void)onClick:(id)sender
{
    // 判断网络状态
    /*if ( kNotReachable == [self getNetWorkState]) {
     [AppDelegate alertWithNetworkError];
     return;
     }*/
    
    if (self.currentCellSelected == sender) {
        return;
    }
    self.previousCellSelected = self.currentCellSelected;
    self.currentCellSelected = (LVCategoryCell *)sender;
    
    self.previousCellSelected.selected = !self.previousCellSelected.selected;
    self.currentCellSelected.selected = !self.currentCellSelected.selected;
    
    if ( _categoryCellDelegate && [_categoryCellDelegate respondsToSelector:@selector(cellClicked:parentContainer:)] ) {
        NSString *tagId = nil;
        for ( int i=0;i<[_categoryCells count];i++ ) {
            LVCategoryCell *cell = [_categoryCells objectAtIndex:i withDefaultValue:nil];
            if ( nil == cell )
                continue;
            if ( cell == self.currentCellSelected ) {
                tagId = [[NSString alloc] initWithString:cell.cateCellInfo.cId];
                break;
            }
        }
        if ( tagId != nil )
            [_categoryCellDelegate cellClicked:tagId parentContainer:self];
        [tagId release];
    }
}

-(void) resetPreviousSelectedState
{
    for ( int i=0;i<[_categoryCells count];i++ ) {
        LVCategoryCell *cell = [_categoryCells objectAtIndex:i withDefaultValue:nil];
        if ( nil == cell )
            continue;
        
        cell.selected = NO;
    }
    self.currentCellSelected = self.previousCellSelected;
    self.previousCellSelected.selected = YES;
}

- (void)dealloc
{
    self.currentCellSelected = nil;
    self.previousCellSelected = nil;
    [self.categoryCells removeAllObjects];
    self.categoryCells = nil;
    
    [super dealloc];
}

@end
